//
//  UIBarButtonItem+SGBarButton.m
//  SGShow
//
//  Created by fanshijian on 15-3-10.
//  Copyright (c) 2015年 fanshijian. All rights reserved.
//

#import "UIBarButtonItem+SGBarButton.h"

@implementation UIBarButtonItem (SGBarButton)

// 左侧 - 返回按钮
+ (UIBarButtonItem *)createBackBarButtonForDelegate:(id)delegate action:(SEL)action {
    // 20 36
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, 50, 44)];
    [backButton addTarget:delegate action:action forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"home-7"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(10, 0, 10, 30)];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
//    backButton.backgroundColor = [UIColor redColor];
    return backItem;
}

// 左侧 - 返回按钮 (带文字)
+ (UIBarButtonItem *)createBackBarButtonForDelegate:(id)delegate title:(NSString *)title action:(SEL)action {
    CGFloat height = 44.0f;
    CGFloat leftMargin = 8.0f;
    UIImage *image = [UIImage imageNamed:@"comm_back"];
    CGSize imgSize = image.size;
    
    CGSize titleSize = BH_MULTILINE_TEXTSIZE(title, [UIFont systemFontOfSize:17.0], CGSizeMake(CGFLOAT_MAX, height), nil);
    CGFloat titleW = titleSize.width+16.0f;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, leftMargin+imgSize.width+titleW, height)];
    [backButton addTarget:delegate action:action forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton.titleLabel setFont:[UIFont systemFontOfSize:17.0]];
    [backButton setTitle:title forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake((height-imgSize.height)/2.0, leftMargin, (height-imgSize.height)/2.0, titleW)];
    [backButton setTitleEdgeInsets:UIEdgeInsetsMake((height-titleSize.height)/2.0, leftMargin+imgSize.width-10.0f, (height-titleSize.height)/2.0, 0)];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    return backItem;
}

// 左侧 - 菜单按钮
+ (UIBarButtonItem *)createMenuBarButtonForDelegate:(id)delegate action:(SEL)action {
    // 52 32
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 60, 44)];
    [leftButton addTarget:delegate action:action forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"btn_menu_list.png"] forState:UIControlStateNormal];
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(14, 17, 14, 17)];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    return leftItem;
}

// 右侧 - 文字按钮
+ (UIBarButtonItem *)createRightWordBarButtonForDelegate:(id)delegate title:(NSString *)title action:(SEL)action {
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 60, 44)];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton setBackgroundColor:[UIColor clearColor]];
    [rightButton addTarget:delegate action:action forControlEvents:UIControlEventTouchUpInside];
    [rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    return rightItem;
}

// 右侧 - 根据文字内容动态生成按钮
+ (UIBarButtonItem *)flexibleCreateRightWordBarButtonForDelegate:(id)delegate title:(NSString *)title action:(SEL)action {
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size =[title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
    [rightButton setFrame:CGRectMake(0, 0, size.width + 30, 44)];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton.titleLabel setTextColor:[UIColor whiteColor]];
    [rightButton addTarget:delegate action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    return rightItem;
}
@end
